//存放后端管理api以及通用／上传api
const express = require('express')
const router = express.Router()
const fs = require('fs')
const multer = require('multer')
const bodyParser = require('body-parser')
const path = require('path')
var upload = multer({ dest: 'upload' })
var http = require("http")
var crypto = require('crypto')
const mysql = require('promise-mysql');


// const pool = mysql.createPool({
//     host: 'localhost',
//     user: 'root',
//     port: '8889',
//     password: '6653145',
//     database: 'demo'
//   });
 
//处理上传的插件
var createFolder = function(folder){
    try{
        fs.accessSync(folder); 
    }catch(e){
        fs.mkdirSync(folder);
    }  
};

//设置上传目录
var uploadFolder = 'upload/';
createFolder(uploadFolder);

// 通过 filename 属性定制文件名等信息
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, uploadFolder); 
         // 保存的路径，备注：需要自己创建
    },
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + Math.floor(Math.random()*100000000));   
        // 将保存文件名设置为 字段名 + 时间戳，比如 logo-1478521468943
    }
});

//初始化multer插件，使用stroage进行转存
var upload = multer({ storage: storage})


//多图上传
router.post('/api/upload', upload.array('file', 6), function(req, res, next){     
    var files = req.files;
    for(var i=0;i<files.length;i++){
     var tmp_path = files[i].path;
     var target_path = './upload/' + files[i].filename+ '.mp3';
     fs.rename(tmp_path,target_path,function(err){
        if(err) throw err;
        fs.unlink(tmp_path,function(){
            if(err) throw err;
            res.json(target_path)
        })
      })       
    }
});

router.get('/api/getInfo',((req, res)=>{
    pool.query("SELECT * FROM user WHERE id = 266").then((rows)=>{
           return rows
    }).then((rows)=>{
       pool.query("SELECT * FROM hydropower WHERE uid='"+rows[0].id +"' ").then((info)=>{ 
        var result = {
            info : rows,
            shuidian:info
        }
        res.send(result)
     })
   }).catch(function(error){
       console.log(error);
   })
}))



router.get('/api/redisses',function(req,res){  
    if(req.session.user=='dubinbin'){
        res.send('已请求过');  
    }else{
        req.session.user = 'dubinbin';  
        req.session.age = '19';
        req.session.uid = '10086';
        res.send(req.session.user);  
    }
})  
  
router.get('/api/checkses',function(req,res){  
    console.log(req.session.user);  
    if(req.session.user==undefined){
        res.send('已过期')
    }else{
        res.send(req.session.id)
    }
})  
// router.get('/api/getInfo',((req, res)=>{
//      pool.query("SELECT * FROM user WHERE id = 266").then((rows)=>{
//             return rows
//      }).then((rows)=>{
//         pool.query("SELECT * FROM hydropower WHERE uid='"+rows[0].id +"' ").then((info)=>{ 
//             return info
//      }).then((info)=>{
//         var result ={
//             info : rows,
//             shuidian:info
//         }
//         res.send(result)
//      })
//     }).catch(function(error){
//         console.log(error);
//     })
// }))


//验证码是否正确
module.exports = router
